<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：互动折扣王-连环折扣-倒计时折扣
// 代码日期：v3.0 2018-6-14
// 作者：杨道泉
// 文件名：QnuiZhekouSaveAction.class.php
// 功能： 发布折扣成功后保存到数据库，CURD操作

class QnuiZhekouSaveController extends QnuiBaseController {
    public function get_active_list(){
        //这里为了不使所有活动列出，应读取数据库中的内容
           $item_area = I("item_area");
        try{
                $PROM = M("PromotionsZhekou");
                $total = $PROM->where(Array("sid"=>session("SHOP_ID")))->count();
                $re = $PROM->where(Array("sid"=>session("SHOP_ID"),"item_area"=>$item_area))->getField("activity_id,description,end_time,name,start_time");
            }catch(\Exception $e){
                $msg = $e->getMessage();
            }
            if(!$msg){
                if(count($re)>0){
                    $resp["activity_list"]["common_item_activity"]=array_values($re);
                }
                $resp["is_success"]=true;
                $resp["total_count"]=$total;
                $resp["code"]=200;
            }else{
                $resp["code"]=910;
                $resp["msg"]=$msg;
            }
        $this->ajaxReturn($resp);
    }
    public function save_active_to_database(){
        //指定商品的折扣
        try{
            $PROM = M("PromotionsZhekou");
            $data["name"] = $this->tagName?$this->tagName:I("tagName");
            $data["description"] = $this->notice?$this->notice:I("notice");
            $data["start_time"] = $this->startTime?$this->startTime:date("Y-m-d H:i:s",strtotime(I("startTime")));
            $data["end_time"] = $this->endTime?$this->endTime:date("Y-m-d H:i:s",strtotime(I("endTime")));
            $data["activity_id"] = $this->activity_id?$this->activity_id:I("activity_id");
            $data["sid"] = session("SHOP_ID");
            $data["item_area"]=I("itemArea");
            if($data["item_area"]=="all"){
                $data["discount_rate"]=I("discountRate")*100;
                $data["decrease_amount"]=I("decreaseAmount")*100;
                }
            $id = $PROM->add($data);
        }catch(\Exception $e) {
            $msg = $e->getMessage();
          }
        if($id){
            $re = new \StdClass();
            $re->code=200;
            $re->id=$id;
        }else{
            $re = new \StdClass();
            $re->code=910;
            $re->msg=$msg;
          }
        $this->ajaxReturn($re);
        
    }
    public function edit_active(){
        try{
            $PROM = M("PromotionsZhekou");
            $data["name"] = I("name");
            $data["start_time"] = date("Y-m-d H:i:s",strtotime(I("start_time")));
            $data["end_time"] = date("Y-m-d H:i:s",strtotime(I("end_time")));
            $re = $PROM->where(Array("sid"=>session("SHOP_ID"),"activity_id"=>I("activity_id")))
                       ->save($data);
            
        }catch(\Exception $e){
              $msg = $e->getMessage();
          }
        if(!$msg){
            $re = new \StdClass();
            $re->code=200;
            $re->id=$id;
        }else{
            $re = new \StdClass();
            $re->code=910;
            $re->msg=$msg;
        }
        $this->ajaxReturn($re);
    }
   public function remove_active_from_database(){
        try{
           $PROM = M("PromotionsZhekou");
           $PROM->where(Array("sid"=>session("SHOP_ID"),"activity_id"=>I("activity_id")))->delete();
        }catch(\Exception $e) {
            $msg = $e->getMessage();
           }
        if($msg){
          $re = new \StdClass();
          $re->code=910;
          $re->msg=$msg;
        }else{
          $re = new \StdClass();
          $re->code=200;
          $re->id=$id;
        }
       $this->ajaxReturn($re);
    }
}